package com.junmeng.jdemo.throttle;


import android.os.Bundle;
import android.view.View;

import com.example.common.base.BaseActivityDelegate;
import com.junmeng.jdemo.databinding.ActivityThrottleBinding;
import com.juunmeng.throttle.ThrottleUtil;

/**
 * 演示防抖/节流工具的使用
 * [throttle: java防抖库](https://gitee.com/hijunmeng/throttle)
 */
public class ThrottleActivity extends BaseActivityDelegate {

    private ActivityThrottleBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityThrottleBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
    }

    public void onClickThrottleCount(View view) {
        if (ThrottleUtil.throttleCount(10, 2000)) {
            showToast("2秒内第10次点击则触发");
        }
    }

    public void onClickThrottleFirst(View view) {
        if (ThrottleUtil.throttleFirst(2000)) {
            showToast("2秒内第一次点击触发");
        }
    }

    public void onClickThrottleFirst2(View view) {
        ThrottleUtil.throttleFirst(() -> {
            showToast("2秒内第一次点击通过回调触发");
        }, 2000);
    }

    public void onClickThrottleLast(View view) {
        ThrottleUtil.throttleLast(() -> {
            showToast("2秒内最后一次点击通过回调触发");
        }, 2000);
    }

    public void onClickThrottleFirstAndLast(View view) {
        ThrottleUtil.throttleFirstAndLast(() -> {
            showToast("3秒内第一次和最后一次点击通过回调触发");
        }, 3000);
    }
}